/*
 * Powered By fanrenqij.com
 * Since 2017 - 2017
 */

package com.fanrenqiji.modules.business.messagerecord.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.Lists;

import com.fanrenqiji.entity.MessageRecordEntity;
import com.fanrenqiji.modules.business.messagerecord.vo.MessageRecordVO;
import com.fanrenqiji.modules.business.messagerecord.dao.MessageRecordDao;
import com.fanrenqiji.modules.business.messagerecord.service.MessageRecordService;

import org.dozer.DozerBeanMapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.fanrenqiji.common.persistence.Page;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.Order;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 消息发送记录业务逻辑实现
 *
 * @author wentao
 */
@Service(value = "messageRecord")
@Transactional(readOnly = true)
public class MessageRecordServiceImpl implements MessageRecordService{

@Autowired
private MessageRecordDao messageRecordDao;

@Autowired
    DozerBeanMapper beanMapper;

/**
 * 插入
 *
 * @param messageRecord
 * 插入的对象
 * @return 插入数据条数
 */
@Transactional
public MessageRecordVO insert(MessageRecordVO messageRecord){
        MessageRecordEntity messageRecordEntity=new MessageRecordEntity();
        beanMapper.map(messageRecord,messageRecordEntity);
        messageRecordDao.save(messageRecordEntity);
        beanMapper.map(messageRecordEntity,messageRecord);
        return messageRecord;
        }


/**
 * 批量插入
 *
 * @param list
 * 数据列表
 * @return 插入数据条数
 */
@Transactional
public void insertList(List<MessageRecordVO>list){
        for(MessageRecordVO messageRecord:list){
        insert(messageRecord);
        }
        }


/**
 * 修改
 *
 * @param messageRecord
 *            修改的数据
 * @return 修改的数据
 */
@Transactional
public MessageRecordVO update(MessageRecordVO messageRecord){
        MessageRecordEntity messageRecordEntity=null;
        if(StringUtils.isNotEmpty(messageRecord.getId())){
        messageRecordEntity=messageRecordDao.findOne(Long.parseLong(messageRecord.getId()));
        }
        if(messageRecordEntity==null){
        messageRecordEntity=new MessageRecordEntity();
        }
        beanMapper.map(messageRecord,messageRecordEntity);
        messageRecordDao.save(messageRecordEntity);
        beanMapper.map(messageRecordEntity,messageRecord);
        return messageRecord;
        }


/**
 * 删除
 *
 * @param id
 *            数据标识
 * @return 删除的数据条数
 */
@Transactional
public void delete(String id){
        messageRecordDao.deleteById(Long.parseLong(id));
        }


/**
 * 查询所有数据
 *
 * @return 数据列表
 */
public List<MessageRecordVO>getAll(){
        List<MessageRecordVO>list=Lists.newArrayList();
        List<MessageRecordEntity>entities=messageRecordDao.findAllList();
        for(MessageRecordEntity messageRecordEntity:entities){
        MessageRecordVO messageRecordVO=new MessageRecordVO();
        beanMapper.map(messageRecordEntity,messageRecordVO);
        list.add(messageRecordVO);
        }
        return list;
        }


/**
 * 通过条件查询数据列表
 *
 * @param messageRecord
 *            查询条件
 * @return 数据列表
 */
public List<MessageRecordVO>getList(MessageRecordVO messageRecord){
        List<MessageRecordVO>list=Lists.newArrayList();
        DetachedCriteria dc=messageRecordDao.createDetachedCriteria();
        dc.add(Restrictions.eq(MessageRecordEntity.DEL_FLAG,MessageRecordEntity.DEL_FLAG_NORMAL));
    /*
    dc.createAlias("messageRecord", "messageRecord"); // as

    dc.add(Restrictions.ne("id", 1L)); // !=

    if (StringUtils.isNotEmpty(messageRecord.getName())) {
        dc.add(Restrictions.like("name", "%" + messageRecord.getName() + "%")); //like
    }
    if (StringUtils.isNotEmpty(messageRecord.getName())) {
        dc.add(Restrictions.eq("name", messageRecord.getName())); //=
    }

	dc.addOrder(Order.asc("office.id")).addOrder(Order.asc("name"));  //排序

    */
        List<MessageRecordEntity>entities=messageRecordDao.find(dc);
        for(MessageRecordEntity messageRecordEntity:entities){
        MessageRecordVO messageRecordVO=new MessageRecordVO();
        beanMapper.map(messageRecordEntity,messageRecordVO);
        list.add(messageRecordVO);
        }
        return list;
        }


/**
 * 通过id查询数据
 *
 * @param id
 *            数据标识
 * @return 数据对象
 */
public MessageRecordVO get(String id){
        MessageRecordVO messageRecord=new MessageRecordVO();
        MessageRecordEntity messageRecordEntity=messageRecordDao.findOne(Long.parseLong(id));
        if(messageRecordEntity==null){
        messageRecordEntity=new MessageRecordEntity();
        }
        beanMapper.map(messageRecordEntity,messageRecord);
        return messageRecord;
        }


/**
 * 根据条件分页查询
 * @param page
 * @param messageRecord
 * @return
 */
public Page<MessageRecordVO>findMessageRecordByPage(Page<MessageRecordVO>page,MessageRecordVO messageRecord){
        Page<MessageRecordEntity>pageEntity=new Page<MessageRecordEntity>(page.getPageNo(),page.getPageSize());
        DetachedCriteria dc=messageRecordDao.createDetachedCriteria();
        dc.add(Restrictions.eq(MessageRecordEntity.DEL_FLAG,MessageRecordEntity.DEL_FLAG_NORMAL));
    /*
        if (StringUtils.isNotEmpty(messageRecord.getName())) {
            dc.add(Restrictions.like("name", "%" + messageRecord.getName() + "%")); //like
        }

    dc.createAlias("messageRecord", "messageRecord"); // as

    dc.add(Restrictions.ne("id", 1L)); // !=


    if (StringUtils.isNotEmpty(messageRecord.getName())) {
        dc.add(Restrictions.eq("name", messageRecord.getName())); //=
    }

    if (!StringUtils.isNotEmpty(page.getOrderBy())) { //排序
	    dc.addOrder(Order.asc("name"))
		  .addOrder(Order.desc("id"));
	}
    */
        if(!StringUtils.isNotEmpty(page.getOrderBy())){
            dc.addOrder(Order.desc("id"));
        }
        pageEntity=messageRecordDao.find(pageEntity,dc);
        beanMapper.map(pageEntity,page);
        List<MessageRecordVO>list=Lists.newArrayList();
        for(MessageRecordEntity messageRecordEntity:pageEntity.getList()){
            MessageRecordVO messageRecordVO=new MessageRecordVO();
            beanMapper.map(messageRecordEntity,messageRecordVO);
            list.add(messageRecordVO);
        }
        page.setList(list);
        return page;
        }


 }
